﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

using HandyControl.Data;
using HandyControl.Tools;
using HandyControl.Themes;

using Stock.Model;

namespace Stock.Core
{
    public static class AppExtensions
    {
        public static void SetSkin(this Application app, int type)
        {
            var skin = (SkinType)type;

            app.Resources.MergedDictionaries.Clear();
            app.Resources.MergedDictionaries.Add(new Theme() { Skin = skin });
            app.Resources.MergedDictionaries.Add(new ResourceDictionary
            {
                Source = new Uri("pack://application:,,,/Stock;component/Resources/Style/App.xaml")
            });

            app.MainWindow?.OnApplyTemplate();
        }

        public static void SetColor(this Application app, AppConfigModel configModel)
        {
            if (!string.IsNullOrEmpty(configModel.ZhangColor))
            {
                app.AddOrUpdateColor(AppConst.RES_KEY_ZHANG_COLOR, configModel.ZhangColor);
            }

            if (!string.IsNullOrEmpty(configModel.DieColor))
            {
                app.AddOrUpdateColor(AppConst.RES_KEY_DIE_COLOR, configModel.DieColor);
            }

            if (!string.IsNullOrEmpty(configModel.SpriteBgColor))
            {
                app.AddOrUpdateColor(AppConst.RES_KEY_SPRITE_BG_COLOR, configModel.SpriteBgColor);
            }
        }

        public static void AddOrUpdateColor(this Application app, string key, string value)
        {
            object colorValue = null;
            try
            {
                colorValue = ColorConverter.ConvertFromString(value);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            if (colorValue == null)
            {
                return;
            }

            if (app.Resources.Contains(key))
            {
                app.Resources.Remove(key);
            }
            app.Resources.Add(key, colorValue);
        }

        public static void AddOrUpdateBrush(this Application app, string key, string colorString)
        {
            Color colorValue = Color.FromRgb(0, 0, 0);
            bool flag = false;
            try
            {
                colorValue = (Color)ColorConverter.ConvertFromString(colorString);
                flag = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                flag = false;
            }
            if (flag)
            {
                app.AddOrUpdateBrush(key, colorValue);
            }
        }

        public static void AddOrUpdateBrush(this Application app, string key, Color color)
        {
            if (app.Resources.Contains(key))
            {
                app.Resources.Remove(key);
            }
            app.Resources.Add(key, new SolidColorBrush(color));
        }

        public static SolidColorBrush GetColorBrush(this Application app, string key)
        {
            if (app.Resources.Contains(key))
            {
                return (SolidColorBrush)app.Resources[key];
            }
            return null;
        }

        public static object GetResource(this Application app, string key)
        {
            if (app.Resources.Contains(key))
            {
                return app.Resources[key];
            }
            return null;
        }

        public static string GetFirstArgs(this Application app)
        {
            if (app is App a)
            {
                var args = a.GetArgs();
                if (args != null && args.Length > 0)
                {
                    return args[0];
                }
            }
            return string.Empty;
        }
    }
}
